#include "mainline_task_dialog.hpp"
#include <stdint.h>
#include <algorithm>
#include <iterator>
#include <vector>

#include "app/app_base.hpp"
#include "data/app_config.hpp"
#include "data/data_helper.hpp"
#include "data/sql_collection_view.hpp"
#include "event/events.hpp"
#include "gui/controls/custom_controls.hpp"
#include "imgui/IconsFontAwesome4.h"
#include "imgui/imgui.h"
#include "imgui/imgui_stdlib.h"
#include "utils/app_utils.hpp"
#include "data/mainline_task_mgr.hpp"
#include "data/sql_stores.hpp"

#include "gui/controls/redmine_controls.hpp"
#include "utils/date_utils.hpp"

#define MAINLINE_TASK_EDIT_DIALOG ICON_FA_PENCIL_SQUARE " 编辑主线任务"

struct MainlineTaskDialog::Data
{
    Data()
    {
        // load cfg
        Reload();
    }
    ~Data()
    {

    }

    void Reload() {
        tags.clear();
        self_copy = StoreLoad(MainlineTaskInfo);
        auto const &all_tags = SqlViewOf(Tag);
        for (auto &id: self_copy.tags) {
            auto it = std::find_if(begin(all_tags), end(all_tags), [id](const Tag &t) { return t.tag_id == id; });
            if (it != end(all_tags))
                tags.emplace_back(*it);
        }
        Save();
    }

    void Save() {
        // 清楚无效的tag
        self_copy.tags.clear();
        std::transform(begin(tags), end(tags), std::back_inserter(self_copy.tags), [](const Tag &t) { return t.tag_id; });
        StoreSave(self_copy);
    }

    MainlineTaskInfo self_copy;
    std::vector<Tag> tags;
    double today_total_hours = 8.0;
};

MainlineTaskDialog::MainlineTaskDialog (): d{new Data}
{
}

MainlineTaskDialog::~MainlineTaskDialog ()
{
    delete d;
}

unsigned MainlineTaskDialog::Flags () const
{
    return eMenu | eDialog;
}

void MainlineTaskDialog::RenderContent (ComponentFlag flag)
{
    if (flag == eDialog)
        Dialog();
    else if (flag == eMenu)
        Menu();
}

const char * MainlineTaskDialog::GetName (ComponentFlag flag)
{
    if (flag == eDialog)
        return MODAL_DIALOG_NAME (MAINLINE_TASK_EDIT_DIALOG);
    return nullptr;
}

using namespace ImGui;

void MainlineTaskDialog::Menu ()
{
    if (BeginMenu (ICON_FA_THUMB_TACK" 任务（主线）"))
    {
        if (MenuItem (ICON_FA_PENCIL_SQUARE " 编辑当前主线任务"))
        {
            // reload config
            d->Reload();
            // show editor dialog
            App->ShowDialog(MAINLINE_TASK_EDIT_DIALOG);
        }
        if (BeginMenu(ICON_FA_CLOCK_O " 补全今日工时"))
        {
            AlignTextToFramePadding();
            TextUnformatted("今日总小时数：");
            SameLine();
            SetNextItemWidth(GetFontSize()*4.5f);
            InputDouble("##TotalHours", &d->today_total_hours, 0, 0, "%.1f");
            SameLine();
            if (Button("生成")) {
                App->Bus()->postpone(AppEvent::FillByMainlineTaskEvent{
                    .total_hour = d->today_total_hours,
                    .date = GetToday(),
                });
                ImGui::CloseCurrentPopup();
            }
            ImGui::EndMenu();
        }
        ImGui::EndMenu();
    }
}

void MainlineTaskDialog::Dialog ()
{
    AlignTextToFramePadding();
    TextUnformatted("任务标题：");
    SameLine();
    InputText("##TaskTitle", &d->self_copy.task_title);
    TextUnformatted("关联问题：");
    SameLine();
    SelectRedMineIssue(&d->self_copy.redmine_issue_id);
    TextUnformatted("关联活动：");
    SameLine();
    SelectRedMineActivity(&d->self_copy.redmine_act_id);
    EditTags(d->tags);

    WindowBottomButtons(2, default_ok_cancel_button_text, [this](int i, void*)
        {
            if (i == 0) // ok
            {
                d->Save();
            }
            ImGui::CloseCurrentPopup();
        }, nullptr);
}

ImVec2 MainlineTaskDialog::DialogSize(ImVec2)
{
    return ImVec2(550, 360);
}
